<?php
/**
 * $Author: BEESSHOP $
 * ============================================================================
 * ============================================================================
 */
namespace app\controllers;

use app\models\NewRecorder;
use PHPUnit\Util\Log\JSON;

class NewController extends BaseController{

    public function actionAdd(){
        $post = \Yii::$app->request->post();
        $id = intval($post['NewRecorder']['id']);
        if($id){
            $model = NewRecorder::find()->where('id=:id',[':id' => $id])->one();
            if(!$model){
                $result =['result'=>0,'data'=>'不存在相关内容，请重新操作'];
                return json_encode($result);
            }
        }else{
            $model = new NewRecorder();
            $model->addtime = time();
        }
        $result = $model->add($post);
        return json_encode($result);
    }


    public function actionList(){
        $post = \Yii::$app->request->post('pagination');
        $page = intval($post['page'])-1;
        $pagesize = intval($post['pagesize']);
        $offset = $page*$pagesize;
        $list = NewRecorder::find();
        $count = $list->count();
        $list = $list->limit($pagesize)->offset($offset)->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            $list[$k]['updatetime'] = date('Y-m-d H:i:s',$v['updatetime']);
        }
        $result = ['result'=>1,'data'=>$list,'count'=>$count];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }


    public function actionOne(){
        $id = \Yii::$app->request->post('id');
        $model = NewRecorder::find()->where('id=:id',[':id' => $id])->asArray()->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }


    public function actionDel(){
        $id = \Yii::$app->request->post('id');
        $model = NewRecorder::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result =['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model->delete();
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }

}